<?php
/**
 * 货币查询控制器
 */
namespace app\manage\controller\coin;

use app\manage\controller\Base;

use app\common\cache\CacheData;

use app\common\query\CoinConfig;

class Handle extends Base
{
	private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = input('post.');
        unset($params['token']);
        $this->params = $params;
    }

    /**
     * 新增货币类型
     */
    public function add(){
        $params = $this->params;

        $result = $this->validate($params,'Coin.manage_add');
        if(true!==$result){
            $this->error($result);
        }

        $coin_name = $params['coin_name'];
        $tag = $params['tag'];

        $query = new \app\common\query\Coin;
        $result = $query->addOneData($coin_name,$tag);
        if(false===$result){
            $this->error($query->getError());
        }else{
            // 更新缓存
            $CACHE = CacheData::getInstance('Coin');
            $CACHE->updateCache();
            // 初始化所有会员该货币账户
            $memberCoinQuery = new \app\common\query\MemberCoin;
            $memberCoinQuery->addNewCoinAccount($result);
            $this->success('货币添加成功');
        }
    }

    /**
     * 编辑
     */
    public function edit(){
        $params = $this->params;

        $result = $this->validate($params,'Coin.manage_edit');
        if(true!==$result){
            $this->error($result);
        }

        $coin_name = $params['coin_name'];
        $tag = $params['tag'];
        $id = $params['id'];

        $query = new \app\common\query\Coin;
        if(false===$query->editOneData($coin_name,$tag,0,$id)){
            $this->error($query->getError());
        }else{
            // 更新缓存
            $CACHE = CacheData::getInstance('Coin');
            $CACHE->updateCache();
            
            $this->success('货币编辑成功');
        }
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function delete(){
        $params = $this->params;

        $result = $this->validate($params,'Coin.manage_del');
        if(true!==$result){
            $this->error($result);
        }
        $id = $params['id'];
        $query = new \app\common\query\Coin;
        if(false===$query->delOneData($id)){
            $this->error($query->getError());
        }else{
            // 更新缓存
            $CACHE = CacheData::getInstance('Coin');
            $CACHE->updateCache();

            $this->success('删除成功');
        }

    }

    /**
     * 添加货币配置
     */
    public function add_config(){
        $params = $this->params;
        
        $result = $this->validate($params, 'Coin.manage_coin_config');
        if(true !== $result) {
            $this->error($result);
        }
        
        if(!isset($params['type'])||!$params['coin_id'])
            $this->error('数据错误');

        // 货币转账不能是相同货币
        if(isset($params['convert_coin_id'])&&$params['coin_id']==$params['convert_coin_id']&&$params['type']=='convert')
            $this->error('参数错误');
        
        // 添加数据
        $query = new CoinConfig;
        if(false===$query -> addOneData($params,$params['type'])){
            $this->error($query->getError());
        }
        // 更新缓存
        $CACHE = CacheData::getInstance('CoinConfig');
        $CACHE->updateCache();
        $this->success('添加成功');
    }

    /**
     * 编辑货币配置
     * @return [type] [description]
     */
    public function edit_config(){
        $params = $this->params;

        $id = $params['id'];
        $config = $params['config'];

        $model = new \app\common\model\CoinConfig;
        $model -> save(['config'=>$config],['id'=>$id]);
        // 更新缓存
        $CACHE = CacheData::getInstance('CoinConfig');
        $CACHE->updateCache();
        $this->success('修改成功');
    }

    /**
     * 根据id删除配置
     * @return [type] [description]
     */
    public function del_config(){
        $params = $this->params;

        $id = $params['id'];

        $model = new \app\common\model\CoinConfig;
        $model -> where(['id'=>$id]) -> delete();
        // 更新缓存
        $CACHE = CacheData::getInstance('CoinConfig');
        $CACHE->updateCache();
        $this->success('删除成功');
    }

    /**
     * 批量删除货币配置
     * @return [type] [description]
     */
    public function del_nums_config(){
        $params = $this->params;

        $ids = $params['data'];
        $model = new \app\common\model\CoinConfig;
        $model -> where(['id'=>$ids])->delete();
        // 更新缓存
        $CACHE = CacheData::getInstance('CoinConfig');
        $CACHE->updateCache();
        $this->success('删除成功');
    }


    /**
     * 等级货币配置
     */

    public function add_level_coin_config(){
        
    }

}
